
import pyautogui
import os
import sys
import datetime
from common.Ultils import BlogDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# from selenium.common.exceptions import UnexpectedAlertPresentException

class BlogLogin:
    #构造函数
    url =""
    dirver=""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        #获取BlogDriver的驱动
        self.driver=BlogDriver.driver
        self.driver.get(self.url)
        self.driver.maximize_window()
    #成功登录的测试
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #登入成功后，根据判断能否访问博客系统页面的元素进行判断
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        # 添加截图
        BlogDriver.getScreeShot()
        #返回页面
        # self.driver.back()
#
#     #登录异常测试——捕获弹窗
#     def LoginFailTest(self):
#
#
#         # self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
#
#         #捕获弹窗
#         # alert = self.driver.switch_to.alert
#         # alert.accept()
#
#         try:
#             self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
#             self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
#
#             self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsa")
#             self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
#
#             self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
#
#             #最多等待5s，直到弹窗出现
#             WebDriverWait(self.driver,5).until(EC.alert_is_present())
#
#             # 切换到弹窗并获取文本
#             alert = self.driver.switch_to.alert
#             alert_text = alert.text
#
#             #截屏
#             BlogDriver.getScreeShot()
#             print(f"弹窗内容：{alert_text}")
#             alert.accept()
#         except Exception as e:
#             print("没有找到弹窗")
#
#
#         # self.driver.quit()
#
#
#
    def LoginFailTest(self):

        # 清空输入框并提交错误信息
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsa")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")

        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # #添加一个延时，让弹窗出现——仍然避免不了save_screenshot的异常抛出
        # WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        # #实现强制性截图
        # try:
        #     BlogDriver.getScreeShot()
        # except UnexpectedAlertPresentException:
        #     print("截图时检测到弹窗，但仍保存截图")
        #     with open("../images/2025-03-28") as f:
        #         f.write(self.driver.get_screenshot_as_png())
        #
        # # 切换到弹窗并获取文本
        # alert = self.driver.switch_to.alert
        # alert_text = alert.text
        # print(f"弹窗内容: {alert_text}")

        # 点击“确定”关闭弹窗
        # alert.accept()

        #显示等待
        WebDriverWait(self.driver, 5).until(EC.alert_is_present())

        #截图的存放路径
        # save_path_name = datetime.datetime.now().strftime("%Y-%m-%d")
        # if not os.path.exists("../images/"+save_path_name):
        #     os.mkdir("../images/"+save_path_name)
        # save_path = "../images/"+save_path_name

        # picturename = sys._getframe().f_code.co_name+"-"+datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")+".png"
        #全屏截屏
        # pyautogui.screenshot(os.path.join(save_path, picturename))

        # 截取区域
        # region_screenshot = pyautogui.screenshot(region=(18, 11, 927, 1003))
        # region_screenshot.save(os.path.join(save_path, picturename))

        # 切换到弹窗并获取文本
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        print(f"弹窗内容: {alert_text}")

        # 点击“确定”关闭弹窗
        alert.accept()

        # 添加截图
        BlogDriver.getScreeShot()

        #错误的账号与错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsa")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("12345")

        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 显示等待
        WebDriverWait(self.driver, 5).until(EC.alert_is_present())

        # 切换到弹窗并获取文本
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        print(f"弹窗内容: {alert_text}")

        # 点击“确定”关闭弹窗
        alert.accept()

        # 添加截图
        BlogDriver.getScreeShot()

        # 正确的账号与错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("12345")

        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 显示等待
        WebDriverWait(self.driver, 5).until(EC.alert_is_present())

        # 切换到弹窗并获取文本
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        print(f"弹窗内容: {alert_text}")

        # 点击“确定”关闭弹窗
        alert.accept()

        # 添加截图
        BlogDriver.getScreeShot()